package es.usal.proyecto.View;

import es.usal.proyecto.Controller.Interfaces.IDrawResponse;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.text.TextPaint;
import android.view.MotionEvent;
import android.view.View;

public class OverlayView extends View {
    
	public IDrawResponse delegate=null;
   	private Paint mRectPaint;
    
    private int mStartX = 0;
    
    public int getmStartX() {
		return mStartX;
	}

	public void setmStartX(int mStartX) {
		this.mStartX = mStartX;
	}
	
	private int mStartY = 0;
	
    public int getmStartY() {
		return mStartY;
	}

	public void setmStartY(int mStartY) {
		this.mStartY = mStartY;
	}

	private int mEndX = 0;
	
    public int getmEndX() {
		return mEndX;
	}

	public void setmEndX(int mEndX) {
		this.mEndX = mEndX;
	}

	private int mEndY = 0;
	
    public int getmEndY() {
		return mEndY;
	}

	public void setmEndY(int mEndY) {
		this.mEndY = mEndY;
	}

	private boolean mDrawRect = false;
    private TextPaint mTextPaint = null;
    
    public int getX0(){
    	
		return Math.min(mStartX, mEndX);
    }
    
    public int getX1(){
    	
		return Math.max(mStartX, mEndX);
    }
    
    public int getY0(){
    	
		return Math.min(mStartY, mEndY);
    }
    
    public int getY1(){
    	
		return Math.max(mStartY, mEndY);
    }

    private int viewHeight;
    public int getViewHeight() {
		return viewHeight;
	}

	public void setViewHeight(int viewHeight) {
		this.viewHeight = viewHeight;
	}

	private int viewWidth;
    
    public int getViewWidth() {
		return viewWidth;
	}

	public void setViewWidth(int viewWidth) {
		this.viewWidth = viewWidth;
	}
	
    public OverlayView(Context context) {  
        super(context);  
        setLongClickable(true);  
        mRectPaint = new Paint();
        mRectPaint.setColor(Color.CYAN);
        mRectPaint.setStyle(Paint.Style.STROKE);
        mRectPaint.setStrokeWidth(5); 

        mTextPaint = new TextPaint();
        mTextPaint.setColor(Color.CYAN);
        mTextPaint.setTextSize(20);
        
    }
    
	@Override
    protected void onDraw(final Canvas canvas) {
        super.onDraw(canvas);
        
        viewHeight = canvas.getHeight();
        viewWidth = canvas.getWidth();

        if (mDrawRect) {
            canvas.drawRect(getX0(), getY0(),
                    getX1(), getY1(), mRectPaint);
            canvas.drawText("  (" + Math.abs(mStartX - mEndX) + ", " + Math.abs(mStartY - mEndY) + ")",
                    Math.max(mEndX, mStartX), Math.max(mEndY, mStartY), mTextPaint);
        }else{
        	canvas.restore();
        }
    }
    @Override
    public boolean onTouchEvent(final MotionEvent event) {

        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                mDrawRect = false;
                mStartX = (int) event.getX();
                mStartY = (int) event.getY();
                invalidate();
                break;

            case MotionEvent.ACTION_MOVE:
                final int x = (int) event.getX();
                final int y = (int) event.getY();

                if (Math.abs(x - mEndX) > 0 || Math.abs(y - mEndY) > 0) {
                    mEndX = x;
                    mEndY = y;
                    invalidate();
                }

                mDrawRect = true;
                break;

            case MotionEvent.ACTION_UP:
            	invalidate();
            	mDrawRect = false;
            	delegate.rectAreaCreated();   

            	
            	break;
            default:
                break;
        }

        return true;
    }
}
